<?php
class TaskNum {
	var $arr_snum;		// Laufende Nummerierung der Tasks in Punktnotation
	var $arr_tanum;		// Tasknummer
	var $arr_talevel;	// Level
	var $arr_taid;		// Interne laufende Nummer im Plan
	var $arr_type;		// Art des Tasks (Folder, ...)
	var $akt_plan;		// Aktuell gespeicherte Plannummer
	var $db;		// Datenbankhandle
	var $max;		// Maximale Anzahl im Array

	function kNumber_($snum, $level, $dir) {
	   $anz = str_wordcount($snum);
	   $kpnum = $snum;

	   # Haben wir weniger Einzelnummern im String, haengen wir ".1" so
	   # lange an, bis wir #level erreicht haben.
	   if ($anz < $level) {
	      while ($anz < $level) {
		 $kpnum .= ".0";
		 $anz++;
	      }

	      if ($dir == -3) {
		 return $kpnum;
	      }
	   }

	   $anz = $level;
	   $anum = explode(".", $kpnum);

	   switch ($dir) {
	      case 0:
	      case -1: $anum[$level-1]++; break;
	      case -2: $anum[$level-1]--; break;
	      case -3: break;
	      default: $anum[$level-1]--;
	   }

	   $num = "";

	   for ($i = 0; $i < $anz; $i++) {
	      if ($i > 0) {
		 $num .= "." . (string)$anum[$i];
	      } else {
		 $num = (string)$anum[$i];
	      }
	   }

	   return $num;
	}

	function TaskNum($pl_num, $db = -1) {
	   global $phase;
	   global $numphase;

	   if (!isset($pl_num) || $pl_num <= 0) {
	      Error("class TaskNum: Ung&uuml;ltige Plannummer wurde &uuml;bergeben!");
	      $this->db = -1;
	      return;
	   }

	   $this->akt_plan = $pl_num;

	   if ($db == -1)
	      $this->db = OpenDB();
	   else
	      $this->db = $db;

	   $query = "select ta_num, ta_level, ta_id, ta_meeting, ta_phase from task where ";
	   $query .= "ta_plnum = $pl_num order by ta_id";

	   if (!($result = QueryDB($this->db, $query))) {
	      $this->db = -1;
	      return;
	   }

	   $anz = numrowsDB($result);
	   $this->max = $anz;
	   $j = 0;
	   $pos = 0;
	   $snum = "1";

	   while ($j < $anz) {
	      $data = fetchDB($result, $j);
	      $ta_num = $data[0];
	      $ta_level = $data[1];
	      $ta_id = $data[2];
	      $ta_meeting = $data[3];
	      $ta_phase = $data[4];

	      $this->arr_tanum[$j] = $ta_num;
	      $this->arr_talevel[$j] = $ta_level;
	      $this->arr_taid[$j] = $ta_id;

	      if ($ta_level == 1) {
	         if (CheckTrue($phase) && CheckTrue($numphase)) {
		    $snum = (string)$ta_phase;
		    $pos = $ta_phase;
		 } else {
		    $pos++;
		    $snum = (string)$pos;
		 }

		 $this->arr_snum[$j] = $snum;
		 $this->arr_type[$j] = "f";
	      } else {
		 $snum = $this->kNumber_($snum, $ta_level, 0);
		 $this->arr_snum[$j] = $snum;

		 if (!$ta_meeting)
		    $this->arr_type[$j] = "t";
		 else
		    $this->arr_type[$j] = "m";

		 if ($j > 0 && $this->arr_type[$j-1] != "m" && $this->arr_talevel[$j-1] < $ta_level)
		    $this->arr_type[$j-1] = "f";
	      }

	      $j++;
	   }
	}

	function GetStatus() {
	   if ($this->db == -1)
	      return false;
	   else
	      return true;
	}

	function Get_tanum($x) {
	   if ($this->db == -1 || $this->max < $x)
	      return 0;

	   return $this->arr_tanum[$x];
	}

	function Get_talevel($x) {
	   if ($this->db == -1 || $this->max < $x)
	      return 0;

	   return $this->arr_tanum[$x];
	}

	function Get_snum($x) {
	   if ($this->db == -1 || $this->max < $x)
	      return "";

	   return $this->arr_snum[$x];
	}

	function Get_taid($x) {
	   if ($this->db == -1 || $this->max < $x)
	      return 0;

	   return $this->arr_taid[$x];
	}

	function Get_type($x) {
	   if ($this->db == -1 || $this->max < $x)
	      return "";

	   return $this->arr_type[$x];
	}
}
